package com.github.m6d21.sso.dao.permission.dao.impl;

import com.github.m6d21.sso.dao.permission.dao.IPermissionDao;
import com.github.m6d21.sso.dao.permission.mapper.PermissionMapper;
import com.github.m6d21.sso.model.permission.entity.Permission;
import com.github.m6d21.sso.model.permission.qo.PermissionQO;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

/**
 *<p>
 *
 *</p>
 *
 * @author liuyang
 * @date 2019/1/4 Fri 17:00:00
 * @since 1.0.0
 */
@Repository("permissionDao")
public class PermissionDaoImpl implements IPermissionDao {

    @Resource
    private PermissionMapper permissionMapper;

    @Override
    public Permission getById(Integer id) {
        if (id == null) {
            return null;
        }
        return permissionMapper.get(id);
    }

    @Override
    public boolean insertPermission(Permission permission) {
        if (permission == null) {
            return false;
        }
        int row = permissionMapper.insertPermission(permission);
        return row == 1;
    }

    @Override
    public boolean insertSelectivePermission(Permission permission) {
        if (permission == null) {
            return false;
        }
        int row = permissionMapper.insertSelectivePermission(permission);
        return row == 1;
    }

    @Override
    public boolean updatePermissionById(Permission permission) {
        if (permission == null || permission.getId() == null) {
            return false;
        }
        int row = permissionMapper.updatePermissionById(permission);
        return row == 1;
    }

    @Override
    public boolean updateSelectivePermissionById(Permission permission) {
        if (permission == null || permission.getId() == null) {
            return false;
        }
        int row = permissionMapper.updateSelectivePermissionById(permission);
        return row == 1;
    }

    @Override
    public List<Permission> find(PermissionQO permissionQO) {
        if (permissionQO == null) {
            permissionQO = new PermissionQO();
        }
        return permissionMapper.find(permissionQO);
    }

    @Override
    public List<Permission> query(PermissionQO permissionQO) {
        if (permissionQO == null) {
            permissionQO = new PermissionQO();
        }
        permissionQO.setDisable(0);
        return permissionMapper.query(permissionQO);
    }

    @Override
    public List<Permission> queryForAdmin(PermissionQO permissionQO) {
        if (permissionQO == null) {
            permissionQO = new PermissionQO();
        }
        return permissionMapper.query(permissionQO);
    }

    @Override
    public boolean deleteById(Integer id) {
        if (id == null) {
            return false;
        }
        int row = permissionMapper.deletePermissionById(id);
        return row == 1;
    }

    @Override
    public boolean deleteByIds(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        int row = permissionMapper.deletePermissionByIds(ids);
        return row == ids.size();
    }
}
